{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import yaml\n",
    "from pymongo import MongoClient\n",
    "from dotenv import load_dotenv\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "def flatten(l):\n",
    "  return [item for sublist in l for item in sublist]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [],
   "source": [
    "load_dotenv()\n",
    "# This gets all env variables starting with RIM_\n",
    "cf = {}\n",
    "for e in os.environ:\n",
    "  if e.startswith('RIM_'):\n",
    "    e_name = e.replace('RIM_', '').lower()\n",
    "    cf[e_name] = os.getenv(e)\n",
    "# This tries to convert port numbers to int\n",
    "cf = {k: (int(v) if v.isnumeric() else v) for k, v in cf.items()}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [],
   "source": [
    "mc = MongoClient(\n",
    "  host=cf['mongo_host'],\n",
    "  port=cf['mongo_port'],\n",
    "  username=cf['mongo_user'],\n",
    "  password=cf['mongo_pass'],\n",
    ")\n",
    "mc._timeout = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'_id': 'foodstuff',\n",
       "  'desc': 'All you can eat or drink',\n",
       "  'subcats': ['greens', 'meat', 'meals', 'beverage', 'liquors']},\n",
       " {'_id': 'greens', 'desc': 'Grows in a garden'},\n",
       " {'_id': 'meat', 'desc': 'Says Moooo'},\n",
       " {'_id': 'meals', 'desc': 'Is full of ingredients'},\n",
       " {'_id': 'beverage', 'desc': 'Is liquid'},\n",
       " {'_id': 'liquors', 'desc': 'Makes you dizzy'},\n",
       " {'_id': 'weapons',\n",
       "  'desc': 'Made for killing',\n",
       "  'subcats': ['melee', 'ranged', 'exotic']},\n",
       " {'_id': 'melee',\n",
       "  'desc': \"To look into someone's eyes\",\n",
       "  'subcats': ['pointed', 'blunt', 'edged']},\n",
       " {'_id': 'ranged', 'desc': 'For the introverts'},\n",
       " {'_id': 'exotic', 'desc': 'Is it a frying pan?'},\n",
       " {'_id': 'pointed', 'desc': 'Spears'},\n",
       " {'_id': 'blunt', 'desc': 'Crushes limbs'},\n",
       " {'_id': 'edged', 'desc': 'Sharp as razor'},\n",
       " {'_id': 'money',\n",
       "  'desc': 'The medium of exchange',\n",
       "  'subcats': ['coins', 'banknotes', 'credit', 'valuables']},\n",
       " {'_id': 'coins', 'desc': 'Usually round'},\n",
       " {'_id': 'banknotes', 'desc': 'Paper shreds'},\n",
       " {'_id': 'credit', 'desc': 'Cards and chips'},\n",
       " {'_id': 'valuables', 'desc': 'Filthy lucre'},\n",
       " {'_id': 'wearables',\n",
       "  'desc': 'Covers your body',\n",
       "  'subcats': ['clothing', 'armor', 'accessories', 'hazmat']},\n",
       " {'_id': 'clothing',\n",
       "  'desc': 'Made of cloth',\n",
       "  'subcats': ['pants', 'jackets', 'shirts', 'hats']},\n",
       " {'_id': 'armor',\n",
       "  'desc': 'Protects you',\n",
       "  'subcats': ['armor vests',\n",
       "   'armor helmets',\n",
       "   'armor plates',\n",
       "   'armor suit',\n",
       "   'armor exo']}]"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with open('../data/item-categories.yaml', 'r') as f:\n",
    "  categories = flatten(list(yaml.safe_load_all(f)))\n",
    "categories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>_id</th>\n",
       "      <th>desc</th>\n",
       "      <th>subcats</th>\n",
       "      <th>parent</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>foodstuff</td>\n",
       "      <td>All you can eat or drink</td>\n",
       "      <td>[greens, meat, meals, beverage, liquors]</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>greens</td>\n",
       "      <td>Grows in a garden</td>\n",
       "      <td></td>\n",
       "      <td>foodstuff</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>meat</td>\n",
       "      <td>Says Moooo</td>\n",
       "      <td></td>\n",
       "      <td>foodstuff</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>meals</td>\n",
       "      <td>Is full of ingredients</td>\n",
       "      <td></td>\n",
       "      <td>foodstuff</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>beverage</td>\n",
       "      <td>Is liquid</td>\n",
       "      <td></td>\n",
       "      <td>foodstuff</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>liquors</td>\n",
       "      <td>Makes you dizzy</td>\n",
       "      <td></td>\n",
       "      <td>foodstuff</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>weapons</td>\n",
       "      <td>Made for killing</td>\n",
       "      <td>[melee, ranged, exotic]</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>melee</td>\n",
       "      <td>To look into someone's eyes</td>\n",
       "      <td>[pointed, blunt, edged]</td>\n",
       "      <td>weapons</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>ranged</td>\n",
       "      <td>For the introverts</td>\n",
       "      <td></td>\n",
       "      <td>weapons</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>exotic</td>\n",
       "      <td>Is it a frying pan?</td>\n",
       "      <td></td>\n",
       "      <td>weapons</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>pointed</td>\n",
       "      <td>Spears</td>\n",
       "      <td></td>\n",
       "      <td>melee</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>blunt</td>\n",
       "      <td>Crushes limbs</td>\n",
       "      <td></td>\n",
       "      <td>melee</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>edged</td>\n",
       "      <td>Sharp as razor</td>\n",
       "      <td></td>\n",
       "      <td>melee</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>money</td>\n",
       "      <td>The medium of exchange</td>\n",
       "      <td>[coins, banknotes, credit, valuables]</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>coins</td>\n",
       "      <td>Usually round</td>\n",
       "      <td></td>\n",
       "      <td>money</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>banknotes</td>\n",
       "      <td>Paper shreds</td>\n",
       "      <td></td>\n",
       "      <td>money</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>credit</td>\n",
       "      <td>Cards and chips</td>\n",
       "      <td></td>\n",
       "      <td>money</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>valuables</td>\n",
       "      <td>Filthy lucre</td>\n",
       "      <td></td>\n",
       "      <td>money</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>wearables</td>\n",
       "      <td>Covers your body</td>\n",
       "      <td>[clothing, armor, accessories, hazmat]</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>clothing</td>\n",
       "      <td>Made of cloth</td>\n",
       "      <td>[pants, jackets, shirts, hats]</td>\n",
       "      <td>wearables</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>armor</td>\n",
       "      <td>Protects you</td>\n",
       "      <td>[armor vests, armor helmets, armor plates, arm...</td>\n",
       "      <td>wearables</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>accessories</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>wearables</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>hazmat</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>wearables</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>pants</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>clothing</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>jackets</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>clothing</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>shirts</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>clothing</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>hats</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>clothing</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>armor vests</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>armor</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>armor helmets</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>armor</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>armor plates</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>armor</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>armor suit</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>armor</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>armor exo</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>armor</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              _id                         desc  \\\n",
       "0       foodstuff     All you can eat or drink   \n",
       "1          greens            Grows in a garden   \n",
       "2            meat                   Says Moooo   \n",
       "3           meals       Is full of ingredients   \n",
       "4        beverage                    Is liquid   \n",
       "5         liquors              Makes you dizzy   \n",
       "6         weapons             Made for killing   \n",
       "7           melee  To look into someone's eyes   \n",
       "8          ranged           For the introverts   \n",
       "9          exotic          Is it a frying pan?   \n",
       "10        pointed                       Spears   \n",
       "11          blunt                Crushes limbs   \n",
       "12          edged               Sharp as razor   \n",
       "13          money       The medium of exchange   \n",
       "14          coins                Usually round   \n",
       "15      banknotes                 Paper shreds   \n",
       "16         credit              Cards and chips   \n",
       "17      valuables                 Filthy lucre   \n",
       "18      wearables             Covers your body   \n",
       "19       clothing                Made of cloth   \n",
       "20          armor                 Protects you   \n",
       "21    accessories                                \n",
       "22         hazmat                                \n",
       "23          pants                                \n",
       "24        jackets                                \n",
       "25         shirts                                \n",
       "26           hats                                \n",
       "27    armor vests                                \n",
       "28  armor helmets                                \n",
       "29   armor plates                                \n",
       "30     armor suit                                \n",
       "31      armor exo                                \n",
       "\n",
       "                                              subcats     parent  \n",
       "0            [greens, meat, meals, beverage, liquors]             \n",
       "1                                                      foodstuff  \n",
       "2                                                      foodstuff  \n",
       "3                                                      foodstuff  \n",
       "4                                                      foodstuff  \n",
       "5                                                      foodstuff  \n",
       "6                             [melee, ranged, exotic]             \n",
       "7                             [pointed, blunt, edged]    weapons  \n",
       "8                                                        weapons  \n",
       "9                                                        weapons  \n",
       "10                                                         melee  \n",
       "11                                                         melee  \n",
       "12                                                         melee  \n",
       "13              [coins, banknotes, credit, valuables]             \n",
       "14                                                         money  \n",
       "15                                                         money  \n",
       "16                                                         money  \n",
       "17                                                         money  \n",
       "18             [clothing, armor, accessories, hazmat]             \n",
       "19                     [pants, jackets, shirts, hats]  wearables  \n",
       "20  [armor vests, armor helmets, armor plates, arm...  wearables  \n",
       "21                                                     wearables  \n",
       "22                                                     wearables  \n",
       "23                                                      clothing  \n",
       "24                                                      clothing  \n",
       "25                                                      clothing  \n",
       "26                                                      clothing  \n",
       "27                                                         armor  \n",
       "28                                                         armor  \n",
       "29                                                         armor  \n",
       "30                                                         armor  \n",
       "31                                                         armor  "
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(categories)\n",
    "parents = []\n",
    "for _, row in df.iterrows():\n",
    "  if type(row['subcats']) == list:\n",
    "    for cat in row['subcats']:\n",
    "      parents.append({\n",
    "        '_id': cat,\n",
    "        'parent': row['_id']\n",
    "      })\n",
    "parents = pd.DataFrame(parents)\n",
    "categories = pd.concat([df.set_index('_id'), parents.set_index('_id')], axis=1, join='outer').reset_index()\n",
    "categories.fillna('', inplace=True)\n",
    "categories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "InsertManyResult(['foodstuff', 'greens', 'meat', 'meals', 'beverage', 'liquors', 'weapons', 'melee', 'ranged', 'exotic', 'pointed', 'blunt', 'edged', 'money', 'coins', 'banknotes', 'credit', 'valuables', 'wearables', 'clothing', 'armor', 'accessories', 'hazmat', 'pants', 'jackets', 'shirts', 'hats', 'armor vests', 'armor helmets', 'armor plates', 'armor suit', 'armor exo'], acknowledged=True)"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mc['models'].drop_collection('items_categories')\n",
    "mc['models']['items_categories'].insert_many(categories.to_dict(orient='records'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'_id': 'Cucumber',\n",
       "  'cat': 'greens',\n",
       "  'desc': 'Makes cats scared',\n",
       "  'opts': ['weight', 'quality', 'shape']},\n",
       " {'_id': 'Tomato',\n",
       "  'cat': 'greens',\n",
       "  'desc': 'Is not green',\n",
       "  'opts': ['weight', 'quality', 'color']},\n",
       " {'_id': 'Beef',\n",
       "  'cat': 'meat',\n",
       "  'desc': 'Once it said Mooo last time',\n",
       "  'opts': ['weight', 'quality', 'part']}]"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with open('../data/items.yaml', 'r') as f:\n",
    "  items = flatten(list(yaml.safe_load_all(f)))\n",
    "items"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'greens': [{'_id': 'Cucumber',\n",
       "   'desc': 'Makes cats scared',\n",
       "   'opts': ['weight', 'quality', 'shape']},\n",
       "  {'_id': 'Tomato',\n",
       "   'desc': 'Is not green',\n",
       "   'opts': ['weight', 'quality', 'color']}],\n",
       " 'meat': [{'_id': 'Beef',\n",
       "   'desc': 'Once it said Mooo last time',\n",
       "   'opts': ['weight', 'quality', 'part']}]}"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "items_sorted = {}\n",
    "for v in items:\n",
    "  vc = v.copy()\n",
    "  del vc['cat']\n",
    "  try:\n",
    "    items_sorted[v['cat']].append(vc)\n",
    "  except:\n",
    "    items_sorted[v['cat']] = [vc]\n",
    "items_sorted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [],
   "source": [
    "for k, v in items_sorted.items():\n",
    "  mc['items'].drop_collection(k)\n",
    "  mc['items'][k].insert_many(v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
